#!/usr/bin/python
# -*- coding: utf-8 -*-

from utils import parse_datetime, parse_html_value, parse_a_href, \
        parse_search_datetime, unescape_html

class Model(object):

    def __init__(self, api=None):
        self._api = api

    def __getstate__(self):
        # pickle
        pickle = dict(self.__dict__)
        del pickle['_api']  # do not pickle the API reference
        return pickle

    @classmethod
    def parse(cls, api, json):
        """Parse a JSON object into a model instance."""
        model = cls(api)
        for k, v in json.items():
            setattr(model, k, v)
        return model

    @classmethod
    def parse_list(cls, api, json_list):
        """
        Parse a list of JSON objects into a result \
        set of model instances.
        """
        results = []
        for obj in json_list:
            results.append(cls.parse(api, obj))
        return results

    @classmethod
    def parse_string(cls, api, data):
        """
        Parse a string result
        """
        raise NotImplementedError


class Token(Model):
    """
    class for parsing token results
    """
    @classmethod
    def parse(cls, api, json):
        """Parse the string result"""
        print json
        token = cls(api)
        for k, v in json.items():
            setattr(token, k, v)
        return token 

class ModelFactory(object):
    """
    Used by parsers for creating instances
    of models. You may subclass this factory
    to add your own extended models.
    """
    model = Model
    token = Token
